package com.hue.netty.day02.four;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufEncoder;

/*
    发送多种数据类型的客户端
 */
public class Client {
    public static void main(String[] args) {
        //创建客户端的工作组
        EventLoopGroup eventExecutors = new NioEventLoopGroup();
        try {
            //设置客户端的启动配置
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(eventExecutors)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline().
                                    //添加protoBuf编码器
                                    addLast(new ProtobufEncoder()).
                                    addLast(new ClientHandler());
                        }
                    });
            //设置连接ip地址和端口号，启动客户端
            ChannelFuture channelFuture = bootstrap.connect("localhost", 7777).sync();
            //监听关闭的通道
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            eventExecutors.shutdownGracefully();
        }

    }
}
